disclaimer: To ensure that the notebook can be run from (more or less) any point, I try to load the relevant functions or modules whenever I use them in a cell. This is generally not good practice as it adds unneccesary overhead
%matplotlib inline
%load_ext autoreload
%autoreload
import numpy as np
checkBoard = np.zeros((9,9))
checkBoard[0::2, 1::2] = 1
checkBoard[1::2, 0::2] = 1
print(checkBoard)
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
plt.imshow(checkBoard, cmap='gray', interpolation='nearest')
plt.show()
from skimage import data
image_of_a_bush = data.lfw_subset()
image_of_a_bush = image_of_a_bush[0,:,:]
#print the #of dimentions, the shape, and the pixel values of the image
print("The number of dimensions of the image is: ", image_of_a_bush.ndim)
print("The size of the image is: ", image_of_a_bush.shape)
print(image_of_a_bush)
plt.figure(figsize=(1,1))
plt.imshow(image_of_a_bush, cmap='gray', interpolation='nearest')
plt.show()
from skimage import data
import matplotlib.pyplot as plt
import numpy as np
image_hist = data.immunohistochemistry()
#check the size of the image
print("The number of dimensions of the image is: ", image_hist.ndim)
print("The size of the image is: ", image_hist.shape)
plt.imshow(image_hist, cmap=plt.cm.gray)
plt.figure(figsize=(15,5))
plt.subplot(131)
plt.gca().set_title('Red channel')
plt.imshow(image_hist[:,:,0], cmap='Reds', interpolation='nearest')
plt.subplot(132)
plt.gca().set_title('Green channel')
plt.imshow(image_hist[:,:,1], cmap='Greens', interpolation='nearest')
plt.subplot(133)
plt.gca().set_title('Blue channel')
plt.imshow(image_hist[:,:,2], cmap='Blues', interpolation='nearest')
plt.show()
#for the moment let's look at only the first color channel
image_hist = image_hist[:,:,0]
plt.gca().set_title('First channel')
plt.imshow(image_hist, cmap=plt.cm.gray)
from skimage.util import invert
inverted_image = invert(image_hist)
plt.figure(figsize=(15,5))
plt.subplot(121)
plt.gca().set_title('original image')
plt.imshow(image_hist, cmap=plt.cm.gray)
plt.subplot(122)
plt.gca().set_title('inverted image')
plt.imshow(inverted_image, cmap=plt.cm.gray)
A gamma correction applies the nonlinear transform $V_{out} = V_{in}^\gamma$.
A log transform applies $V_{out} = log(V_{in}+1)$.
A sigmoid transform applies $V_{out} = \frac{1}{1+e^{gain\cdot(\text{cutoff}-V_{in})}}$.
Equalization transforms the intensity histogram of an image to a uniform distribution. It often enhances the contrast of the image
CLAHE works similarly to equalization thats applied separately to different regions of the image.
Try to apply these by calling the relevant function from skimage.exposure, or by direct calculation.
Play with the different parameters and see how they change the output.
from skimage import exposure
# apply gamma scaling with gamma=2
gamma=2
gamma_corrected = exposure.adjust_gamma(image_hist, gamma)
# apply logarithmic scaling
logarithmic_corrected = exposure.adjust_log(image_hist)
# apply sigmoidal scaling with cutoff=0.4
cutoff = 0.4
sigmoid_corrected = exposure.adjust_sigmoid(image_hist, cutoff=cutoff)
# equalize
equalize_corrected = exposure.equalize_hist(image_hist)
# apply Contrast Limited Adaptive Histogram Equalization (CLAHE)
CLHA_corrected = exposure.equalize_adapthist(image_hist)
plt.figure(figsize=(15,10))
plt.subplot(231)
plt.gca().set_title('original')
plt.imshow(image_hist, cmap=plt.cm.gray)
plt.subplot(232)
plt.gca().set_title('gamma corrected')
plt.imshow(gamma_corrected, cmap=plt.cm.gray)
plt.subplot(233)
plt.gca().set_title('log corrected')
plt.imshow(logarithmic_corrected, cmap=plt.cm.gray)
plt.subplot(234)
plt.gca().set_title('sigmoid')
plt.imshow(sigmoid_corrected, cmap=plt.cm.gray)
plt.subplot(235)
plt.gca().set_title('equalized')
plt.imshow(equalize_corrected, cmap=plt.cm.gray)
plt.subplot(236)
plt.gca().set_title('CLHA corrected')
plt.imshow(CLHA_corrected, cmap=plt.cm.gray)
This procedure is formally a convolution and is marked by an asterisk: $I_o = I_i\ast f$.
side note: since a convolution in the spatial domain is equivalent to multiplication in the frequency domain. Sometimes it is more computationally reasonable to calculate these in fourier space.
side side note: filtering can also be performed in the frequency domain by directly removing a set of frequencies from an image.
Example, local average: 
Try to change the value of sigma (width of the gaussian) to see how the output changes.
import matplotlib.pyplot as plt
import numpy as np
from skimage import filters
image_hist = data.immunohistochemistry()
sigma = 2
gauss_filtered_img = filters.gaussian(image_hist, sigma=sigma, multichannel=True)
plt.figure(figsize=(15,8))
plt.subplot(121)
plt.gca().set_title('original image')
plt.imshow(image_hist, cmap=plt.cm.gray)
plt.subplot(122)
plt.gca().set_title('response, gaussian smoothing')
plt.imshow(gauss_filtered_img, cmap=plt.cm.gray)
filter_blurred_f = filters.gaussian(gauss_filtered_img, sigma=0.5, multichannel=False)
alpha = 3
sharpened = gauss_filtered_img + alpha * (gauss_filtered_img - filter_blurred_f)
plt.figure(figsize=(15,8))
plt.subplot(121)
plt.gca().set_title('input - blury image')
plt.imshow(gauss_filtered_img, cmap=plt.cm.gray)
plt.subplot(122)
plt.gca().set_title('sharpened')
plt.imshow(sharpened, cmap=plt.cm.gray)
$G = \sqrt{G_x^2+G_y^2}$



# sobel magnitude
filtered_img = filters.sobel(image_hist[:,:,2])
# sobel horizontal
filtered_img_h = filters.sobel_h(image_hist[:,:,2])
# sobel vertical
filtered_img_v = filters.sobel_v(image_hist[:,:,2])
plt.figure(figsize=(15,16))
plt.subplot(221)
plt.gca().set_title('input image')
plt.imshow(image_hist[:,:,2], cmap=plt.cm.gray)
plt.subplot(222)
plt.gca().set_title('sobel filter response - magnitude')
plt.imshow(filtered_img, cmap=plt.cm.gray)
plt.subplot(223)
plt.gca().set_title('sobel filter response - horizontal edges')
plt.imshow(np.abs(filtered_img_h), cmap=plt.cm.gray)
plt.subplot(224)
plt.gca().set_title('sobel filter response - vertical edges')
plt.imshow(np.abs(filtered_img_v), cmap=plt.cm.gray)
import matplotlib.pyplot as plt
import numpy as np
#dimensions in x and y
y = 512
x = 512
#position of center
centY = np.ceil(y/2)
centX = np.ceil(x/2)
#create the grid
yy,xx = np.indices((y,x))
#create radial distance map
radialDist = np.sqrt((xx-centX)**2 + (yy - centY)**2)
#display
plt.gca().set_title('Radial distance')
plt.imshow(radialDist, cmap='gray', interpolation='nearest')
plt.show()
circ1 = radialDist < 100
plt.show()
plt.gca().set_title('Circle with radius 100')
plt.imshow(circ1, cmap='inferno', interpolation='nearest')
plt.gca().set_title('Masked first channel')
plt.imshow(image_hist[:,:,2]*circ1, cmap=plt.cm.gray)
inverted_mask = invert(circ1)
plt.figure(figsize=(15,5))
plt.subplot(121)
plt.gca().set_title('inverted mask')
plt.imshow(inverted_mask, cmap=plt.cm.gray)
plt.subplot(122)
plt.gca().set_title('inverted masked image')
plt.imshow(image_hist[:,:,2]*inverted_mask, cmap=plt.cm.gray)
Just for closure, let's see what happens when we look at the full RGB image and try to apply the mask
image = data.immunohistochemistry()
plt.imshow(image*circ1, cmap=plt.cm.gray)
Whoops. Seems like something is wrong. Our problem is that numpy didn't know how to multiply a 512x512x3 with a 512x512 mask. Numpy makes solving this very easy by adding a singleton dimension (look up broadcasting in your spare time).
image = data.immunohistochemistry()
plt.gca().set_title('Masked image')
plt.imshow(image*np.expand_dims(circ1,2), cmap=plt.cm.gray)

#this function from skimage converts images of integer types into floats, which are easier to work with.
import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
from skimage import data
# First, let's create a noisy image of blobs
image_blobs = img_as_float(data.binary_blobs(length=512, seed=1))
sigma = 0.22
image_blobs += np.random.normal(loc=0, scale=sigma, size=image_blobs.shape)
print("The number of dimensions of the image is: ", image_blobs.ndim)
print("The size of the image is: ", image_blobs.shape)
plt.imshow(image_blobs, cmap=plt.cm.gray)
plt.hist(image_blobs.flatten(),bins=250)
plt.show()
Pick an appropriate threshold, by eye, and see if you can remove the background. What happens when you increase or decrease the threshold?
thresh = 0.5
mask = image_blobs>thresh
plt.figure(figsize=(15,5))
plt.subplot(131)
plt.gca().set_title('original')
plt.imshow(image_blobs, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(132)
plt.gca().set_title('mask')
plt.imshow(mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(133)
plt.gca().set_title('masked image')
plt.imshow(image_blobs*mask, interpolation='nearest', cmap=plt.cm.gray)
We can try and use what we learned before about filtering to clean up our results. What filter should we use?
from skimage import filters
thresh = 0.5
mask = filters.gaussian(image_blobs, sigma=1)>thresh
plt.figure(figsize=(15,5))
plt.subplot(131)
plt.gca().set_title('original')
plt.imshow(image_blobs, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(132)
plt.gca().set_title('mask')
plt.imshow(mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(133)
plt.gca().set_title('masked image')
plt.imshow(image_blobs*mask, interpolation='nearest', cmap=plt.cm.gray)
It's usually a good idea before creating a mask to despeckle an image using a narrow gaussian filter!
Morphology is a broad set of image processing operations that process images based on shapes. In a morphological operation, each pixel in the image is adjusted based on the value of other pixels in its neighborhood. By choosing the size and shape of the neighborhood, you can construct a morphological operation that is sensitive to specific shapes in the input image. (explanation from Mathworks)
Morphological operations are based around a structuring element, which is a small binary image, often of a disk or a square. The structuring element is positioned at all possible locations in the image and it is compared with the corresponding neighbourhood of pixels. Some operations test whether the element "fits" within the neighbourhood, while others test whether it "hits" or intersects the neighbourhood.
Common operations for image processing
Erosion - output image =1 wherever the structuring element fits (erodes the mask)
Dilation - output image =1 wherever the structuring element hits (expands the mask)
Opening - Erosion followed by dilation (opens gaps in spots where the mask is weakly connected)
Closing - Dilation followed by erosion (closes holes in the mask)
A very thorough explanation of morphological operationscould be found here
from skimage.morphology import erosion, dilation, opening, closing
from skimage.morphology import disk
#define a "disk" structuring element
selem = disk(10)
erosion_mask = erosion(mask, selem)
dilation_mask = dilation(mask, selem)
opening_mask = opening(mask, selem)
closing_mask = closing(mask, selem)
plt.figure(figsize=(15,10))
plt.subplot(231)
plt.gca().set_title('mask')
plt.imshow(mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(232)
plt.gca().set_title('erosion')
plt.imshow(erosion_mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(233)
plt.gca().set_title('dilation')
plt.imshow(dilation_mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(235)
plt.gca().set_title('opening')
plt.imshow(opening_mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(236)
plt.gca().set_title('closing')
plt.imshow(closing_mask, interpolation='nearest', cmap=plt.cm.gray)
import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg
#this is how we load an image from the hard drive
image_nuclei = img_as_float(mpimg.imread("../Data/xy040-1.png"))
fig = plt.figure(num=None, figsize=(7.1, 4.6), dpi=80, facecolor='w', edgecolor='k')
print("The number of dimensions of the image is: ", image_nuclei.ndim)
print("The size of the image is: ", image_nuclei.shape)
plt.imshow(image_nuclei, cmap=plt.cm.gray, vmin=0, vmax=0.01)
plt.hist(image_nuclei.flatten(),bins=250)
plt.xlim((0, 0.02))
plt.show()
thresh = 0.003
#remember to despeckle before creating a mask!
mask = filters.gaussian(image_nuclei, sigma=1)>thresh
plt.figure(figsize=(8,15))
plt.subplot(311)
plt.gca().set_title('original')
plt.imshow(image_nuclei, interpolation='nearest', cmap=plt.cm.gray, vmin=0, vmax=0.01)
plt.subplot(312)
plt.gca().set_title('mask')
plt.imshow(mask, interpolation='nearest', cmap=plt.cm.gray)
plt.subplot(313)
plt.gca().set_title('masked image')
plt.imshow(image_nuclei*mask, interpolation='nearest', cmap=plt.cm.gray, vmin=0, vmax=0.01)
Algorithm:

The algorithm exhaustively searches for the threshold that minimizes the intra-class variance, defined for a given threshold $T$ as a weighted sum of variances of the two classes: $\sigma^2_w(T)=\omega_0(T)\sigma^2_0(T)+\omega_1(T)\sigma^2_1(T)$
For 2 classes, minimizing the intra-class variance is equivalent to maximizing inter-class variance, which is much easier to calculate: \begin{align} \sigma^2_b(T) & =\sigma^2-\sigma^2_w(T)=\omega_0(\mu_0-\mu_T)^2+\omega_1(\mu_1-\mu_T)^2 \\ & =\omega_0(T) \omega_1(T) \left[\mu_0(T)-\mu_1(T)\right]^2 \end{align}

Algorithm:

note: Triangle thresholding is good for situations where the image is mostly background, and there is no clear "peak" of bright pixels.
from skimage import filters
meanThresh = filters.threshold_mean(image_nuclei)
print(meanThresh)
OtsuThresh = filters.threshold_otsu(image_nuclei)
print(OtsuThresh)
TriThresh = filters.threshold_triangle(image_nuclei)
print(TriThresh)
fig = plt.figure(num=None, figsize=(12, 8), dpi=80)
ax1 = fig.add_axes([0.1,0.6,0.4,0.4])
ax1.hist(image_nuclei.flatten(),bins=250)
ax1.axvline(meanThresh, color='g', linestyle='--')
ax1.axvline(OtsuThresh, color='r', linestyle='--')
ax1.axvline(TriThresh, color='k', linestyle='--')
ax1.legend(['mean' ,'otsu', 'triangle'])
ax1.set_title('histogram')
ax2 = fig.add_axes([0.6,0.6,0.4,0.4])
mask_mean = filters.gaussian(image_nuclei, sigma=1)>meanThresh
ax2.imshow(mask_mean)
ax2.set_title('Iterative mean')
ax2.set_axis_off()
ax2 = fig.add_axes([0.1,0.1,0.4,0.4])
mask_otsu = filters.gaussian(image_nuclei, sigma=1)>OtsuThresh
ax2.imshow(mask_otsu)
ax2.set_title('Otsu')
ax2.set_axis_off()
ax2 = fig.add_axes([0.6,0.1,0.4,0.4])
mask_tri = filters.gaussian(image_nuclei, sigma=1)>TriThresh
ax2.imshow(mask_tri)
ax2.set_title('Triangle')
ax2.set_axis_off()
Let's compare the results from a global (Otsu) and a local threshold.
from skimage import data
image = data.page()
fig = plt.figure(num=None, figsize=(12, 8), dpi=80)
#global thresholding
threshGlobal = filters.threshold_otsu(image)
ax1 = fig.add_axes([0.1,0.6,0.4,0.4])
ax1.set_title('mask - Otsu threshold')
plt.imshow(image ,cmap='gray')
ax2 = fig.add_axes([0.6,0.6,0.4,0.4])
ax2.set_title('mask - Otsu threshold')
plt.imshow(image>threshGlobal,cmap='gray')
#local thresholding
#Try and change this number and see what happens
block_size = 81
threshLocal = filters.threshold_local(image, block_size)
ax1 = fig.add_axes([0.1,0.2,0.4,0.4])
ax1.imshow(threshLocal,cmap='gray')
ax1.set_title('local threshold map')
ax2 = fig.add_axes([0.6,0.2,0.4,0.4])
ax2.set_title('mask - Local threshold')
plt.imshow(image>threshLocal,cmap='gray')

import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg
from skimage import filters
image_nuclei = img_as_float(mpimg.imread("../Data/xy040-1.png"))
TriThresh = filters.threshold_triangle(image_nuclei)
#despeckle
mask = filters.gaussian(image_nuclei, sigma=1)>TriThresh
$\begin{bmatrix} 1 & 1 & 0 & 0 & 2\\ 1 & 1 & 0 & 2 & 2\\ 0 & 0 & 0 & 0 & 0\\ 0 & 3 & 0 & 4 & 4\\ 0 & 0 & 0 & 4 & 4\\ \end{bmatrix}$
from skimage import measure
labels = measure.label(mask)
plt.figure(figsize=(12,5))
plt.subplot(121)
plt.imshow(mask, cmap='gray')
plt.subplot(122)
plt.imshow(labels, cmap='nipy_spectral')
i=43
mask_of_CC_i = labels==i
plt.figure(figsize=(10,5))
plt.imshow(mask_of_CC_i, cmap='gray')
i=87
mask_of_CC_i = labels==i
plt.imshow(mask_of_CC_i, cmap='gray')
from skimage.morphology import erosion, dilation, opening, closing
from skimage.morphology import disk
#define a "disk" structuring element
selem1 = disk(10)
selem2 = disk(7)
plt.figure(figsize=(15,10))
plt.subplot(121)
plt.gca().set_title('original')
plt.imshow(mask, cmap='nipy_spectral')
plt.subplot(122)
plt.gca().set_title('opening')
plt.imshow(dilation(erosion(mask, selem1),selem2), interpolation='nearest', cmap='nipy_spectral')
The watershed transformation treats the image it operates upon like a topographic map, with the brightness of each point representing its height, and finds the lines that run along the tops of ridges.

More precisely, the algorithm goes as follows:

Let's start with a very naive application. We will invert the image, and then simply apply the watershed function from the scikit-image morphology module. The function returns a labeled image.
import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg
from skimage import filters
from skimage.util import invert
from skimage.morphology import watershed
image_nuclei = img_as_float(mpimg.imread("../Data/xy040-1.png"))
#invert image
image_to_watershed = invert(image_nuclei)
#Calculate watershed transform
labels_naive = watershed(image_to_watershed, watershed_line = 1)
plt.figure(figsize=(15,10))
#let's look at all the boundaries
plt.figure(figsize=(12,20))
plt.subplot(211)
plt.gca().set_title('image fed to watershed')
plt.imshow(image_to_watershed, cmap='gray')
plt.subplot(212)
plt.gca().set_title('watershed result')
plt.imshow(filters.sobel(labels_naive), cmap='nipy_spectral')
Noise generates a ton of local minima. Each gets its own basin. This leads to massive oversegmentation.

The first thing we'll do is to apply the mask that we found before. This is simply done by adding a mask argument to the watershed function.
import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg
from skimage import filters
from skimage.util import invert
from skimage.morphology import watershed
image_nuclei = img_as_float(mpimg.imread("../Data/xy040-1.png"))
TriThresh = filters.threshold_triangle(image_nuclei)
mask = filters.gaussian(image_nuclei, sigma=1)>TriThresh
#mask and invert image
image_to_watershed = image_nuclei*mask
image_to_watershed = invert(image_to_watershed)
#Calculate watershed transform
#pass the mask to the watershed function so it avoids segmenting the BG
labels_masked = watershed(image_to_watershed, watershed_line = 1, mask=mask)
#let's look at all the boundaries
plt.figure(figsize=(12,20))
plt.subplot(211)
plt.gca().set_title('image fed to watershed')
plt.imshow(image_to_watershed, cmap='gray')
plt.subplot(212)
plt.gca().set_title('watershed result')
plt.imshow(filters.sobel(labels_masked), cmap='nipy_spectral')
Let's try to smoothen the image and get rid of the many local minima. How wide should the gaussian kernel be?
import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg
from skimage import filters
from skimage.util import invert
from skimage.morphology import watershed, thin
image_nuclei = img_as_float(mpimg.imread("../Data/xy040-1.png"))
TriThresh = filters.threshold_triangle(image_nuclei)
mask = filters.gaussian(image_nuclei, sigma=1)>TriThresh
#mask, smooth, and invert the image
sigma_for_smoothing = 4
image_to_watershed = image_nuclei*mask
image_to_watershed = filters.gaussian(image_to_watershed, sigma=sigma_for_smoothing)
image_to_watershed = invert(image_to_watershed)
#Calculate watershed transform
#pass the mask to the watershed function so it avoids segmenting the BG
labels_masked_smooth = watershed(image_to_watershed, watershed_line = 1, mask=mask)
#let's look at all the boundaries
plt.figure(figsize=(12,20))
plt.subplot(211)
plt.gca().set_title('image fed to watershed')
plt.imshow(image_to_watershed, cmap='gray')
plt.subplot(212)
plt.gca().set_title('watershed result')
plt.imshow(filters.sobel(labels_masked_smooth), cmap='nipy_spectral')
import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_float
import matplotlib.image as mpimg
from skimage import filters
from skimage import measure
from skimage.util import invert
from skimage.morphology import watershed
from skimage.feature import peak_local_max
image_nuclei = img_as_float(mpimg.imread("../Data/xy040-1.png"))
mask = filters.gaussian(image_nuclei, sigma=1)>TriThresh
#mask, smooth, and invert the image
sigma_for_smoothing = 5
image_to_watershed = image_nuclei*mask
image_to_watershed = filters.gaussian(image_to_watershed, sigma=sigma_for_smoothing)
image_to_watershed = invert(image_to_watershed)
#find local peaks to use as seeds
MaskedImagePeaks = peak_local_max(-image_to_watershed, footprint=np.ones((30, 30)), threshold_abs=None, threshold_rel=None, exclude_border=True, indices=False).astype('int')
#create disk structuring element of radius 5
selem = disk(5)
#dilate local peaks so that close ones merge
peakMask = dilation(MaskedImagePeaks,selem)
# label local peak regions to find initial markers
markers = measure.label(peakMask)
#pass the *markers* argument to the watershed function
labels_localmax_markers = watershed(image_to_watershed,markers, watershed_line = 1, mask=mask)
#let's look at all the boundaries
plt.figure(figsize=(12,20))
plt.subplot(211)
plt.gca().set_title('image fed to watershed')
plt.imshow(image_to_watershed-peakMask, cmap='gray')
plt.clim((0.95, 1))
plt.subplot(212)
plt.gca().set_title('watershed result')
plt.imshow(filters.sobel(labels_localmax_markers), cmap='nipy_spectral')

The length of the list is equal to the total number of objects detected.
from skimage import measure
#We use regionprops
props = measure.regionprops(labels_localmax_markers,image_nuclei)
#how many total connected components did we get?
print(len(props))
areas = [r.area for r in props]
intensities = [r.mean_intensity for r in props]
#let's look at all the boundaries
plt.figure(figsize=(12,6))
plt.subplot(121)
plt.gca().set_title('areas')
plt.hist(areas)
plt.subplot(122)
plt.gca().set_title('intensities')
plt.hist(intensities)
import pandas as pd
def scalar_attributes_list(im_props):
"""
Makes list of all scalar, non-dunder, non-hidden
attributes of skimage.measure.regionprops object
"""
attributes_list = []
for i, test_attribute in enumerate(dir(im_props[0])):
#Attribute should not start with _ and cannot return an array
#does not yet return tuples
if test_attribute[:1] != '_' and not\
isinstance(getattr(im_props[0], test_attribute), np.ndarray):
attributes_list += [test_attribute]
return attributes_list
def regionprops_to_df(im_props):
"""
Read content of all attributes for every item in a list
output by skimage.measure.regionprops
"""
attributes_list = scalar_attributes_list(im_props)
# Initialise list of lists for parsed data
parsed_data = []
# Put data from im_props into list of lists
for i, _ in enumerate(im_props):
parsed_data += [[]]
for j in range(len(attributes_list)):
parsed_data[i] += [getattr(im_props[i], attributes_list[j])]
# Return as a Pandas DataFrame
return pd.DataFrame(parsed_data, columns=attributes_list)
props_df = regionprops_to_df(props)
props_df
from skimage import measure
from skimage import img_as_float
import matplotlib.image as mpimg
#load another channel
image_2ndChannel = img_as_float(mpimg.imread("../Data/xy040-2.png"))
# extract regionprops using labels_localmax_markers mask from image_2ndChannel
props_other_channel = measure.regionprops(labels_localmax_markers,image_2ndChannel)
plt.figure(figsize=(12,6))
plt.subplot(121)
plt.gca().set_title('Nuclei')
plt.imshow(image_nuclei, cmap='gray')
plt.subplot(122)
plt.gca().set_title('other channel')
plt.imshow(image_2ndChannel, cmap='gray')
mean_2nd_channel = [r.mean_intensity for r in props_other_channel]
max_2nd_channel = [r.max_intensity for r in props_other_channel]
min_2nd_channel = [r.min_intensity for r in props_other_channel]
plt.gca().set_title('intensities of 2nd channel')
plt.hist(mean_2nd_channel)
props_df['mean_intensity_ch2'] = mean_2nd_channel
props_df['max_intensity_ch2'] = max_2nd_channel
props_df['min_intensity_ch2'] = min_2nd_channel
props_df